package com.itheima.tlias84.controller;

import com.itheima.tlias84.dto.SelectStudentByIdDTO;
import com.itheima.tlias84.po.InsertStudentParam;
import com.itheima.tlias84.po.SelectStudentPageParam;
import com.itheima.tlias84.po.UpdateScoreByIdParam;
import com.itheima.tlias84.po.UpdateStudentParam;
import com.itheima.tlias84.service.StudentService;
import com.itheima.tlias84.utils.ResultUtil;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/students")
public class StudentController {
    @Resource
    StudentService studentService;

    @GetMapping
    public ResultUtil selectStudentByPage(SelectStudentPageParam param){
        return studentService.selectStudentByPage(param);
    }
    @GetMapping("/{id}")
    public ResultUtil selectStudentById(@PathVariable("id") Integer id){
        SelectStudentByIdDTO selectStudentByIdDTO = studentService.selectStudentById(id);
        return ResultUtil.success(selectStudentByIdDTO);
    }

    @PutMapping
    public ResultUtil updateStudent(@RequestBody UpdateStudentParam param){
        int i = studentService.updateStudent(param);
        return i==0?ResultUtil.fail("更新失败"):ResultUtil.success("更新成功");
    }

    @PostMapping
    public ResultUtil insertStudent(@RequestBody InsertStudentParam param){
        int i = studentService.insertStudent(param);
        return  i == 0? ResultUtil.fail("插入失败"):ResultUtil.success("插入成功");
    }

    @DeleteMapping("/{ids}")
    public ResultUtil batchDelete(@PathVariable("ids") List<Integer> ids){
        if (ids.isEmpty()){
            return ResultUtil.fail("请先选择的id");
        }
        int i = studentService.batchDelete(ids);
        return ResultUtil.success(i);
    }

    @PutMapping("violation/{id}/{score}")
    public ResultUtil updateScore(@PathVariable("id") Integer id,@PathVariable("score") Integer score){
        int i = studentService.updateScore(id,score);
        return i==0?ResultUtil.fail():ResultUtil.success();
    }
}
